/*
 * @description: vite2配置
 * @Author: Islands
 * @Date: 2024-06-14 15:26:53
 * @LastEditTime: 2024-06-22 17:03:19
 */
import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
//element-plus按需要引入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import ElementPlus from 'unplugin-element-plus/vite'
// Element自动导入 图标
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
//unoCss 原子样式
import unoCss from 'unocss/vite';



//vite2官网地址: https://vitejs.dev/config/
export default defineConfig(async ({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), '');
  return {
    plugins: [
      vue(),
      AutoImport({
        // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
        imports: ['vue'],
        resolvers: [
          ElementPlusResolver(),
          // 自动导入图标组件
          IconsResolver({
            prefix: 'Icon',
          }),

        ],
      }),
      Components({
        extensions: ['vue', 'md'],
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
        dts: 'src/components.d.ts',
        resolvers: [
          //自动导入 Element Plus 组件
          ElementPlusResolver({
            // 自动引入修改主题色添加这一行，使用预处理样式
            importStyle: 'sass',
          }),
          //自动注册图标组件
          IconsResolver({
            enabledCollections: ['ep'],
          })
        ],
      }),
      Icons({
        autoInstall: true,
      }),
      ElementPlus({
        useSource: true,
      }),
      unoCss(),
    ],
    //环境变量
    server: {
      host: env.VITE_HOST,
      port: Number(env.VITE_PORT),
      open: true,
      proxy: {
        '/api': {
          target: env.AGENT_HOST,
          changeOrigin: true,
          rewrite: (path: any) => path.replace(/^\/api/, '')
        },
      },
    },
    //别名配置
    resolve: {
      alias: [
        { find: '@', replacement: resolve(__dirname, '/src') },
      ]
    },
    //css与处理器
    module: {
      rules: [
        { test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'] },
      ]
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
              @use "@/assets/scss/element-plus/light.scss" as *;
              @use "element-plus/theme-chalk/src/dark/css-vars.scss" as *;
              @use "@/assets/scss/global/index.scss" as *;
              @use "@/assets/scss/mixin/index.scss" as *;
              @use "@/assets/scss/adapt/index.scss" as *;
                @use "@/assets/scss/element-plus/custom.scss" as *;
              
          `
        }
      }
    },
  }

})
